//
//  RecommendNews.swift
//  newsApp
//
//  Created by Doo Psyist on 2020/5/15.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import SwiftUI

struct RecommendNews: View {
    var courses = courseData
    var body: some View {
        VStack {
            Text("Recommend")
                .font(.system(size: 45, design: .rounded))
                .foregroundColor(Color.orange)
                .bold()
                .shadow(radius: 1)
            ScrollView(.horizontal, showsIndicators: false) {
                HStack {
                    ForEach(courseData) {item in
                        CourseView(title: item.title, content: item.content, color: item.color, newsListItem: item.newsListItem).padding()
                    }
                }
            }
        }
    }
}

struct RecommendNews_Previews: PreviewProvider {
    static var previews: some View {
        RecommendNews()
    }
}

struct CourseView : View {
    var title = ""
    var content = ""
    var color = Color.blue
    var newsListItem: NewsListItem;
    var body: some View {
        return VStack(alignment: .leading) {
            Text(title)
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .padding(20)
            Text(content)
                .font(.headline)
                .fontWeight(.bold)
                .foregroundColor(.white)
                .padding(20)
            MapView(newsListItem: newsListItem)
                .frame(height:300)
                .padding(.top,0)
            Spacer()
        }
        .background(color)
        .cornerRadius(10)
        .frame(width: 360, height: 480)
        .shadow(color:Color.black, radius: 20, x: 0, y: 20)
    }
}

struct Course : Identifiable {
    var id = UUID()
    var cid: Int
    var title: String
    var content: String
    var color: Color
    var newsListItem: NewsListItem
}

let courseData = [
    Course(cid: newsList[0].id, title: newsList[0].title, content: newsList[0].summary, color: Color.blue, newsListItem: newsList[0]),
    Course(cid: newsList[1].id, title: newsList[1].title, content: newsList[1].summary, color: Color.red, newsListItem: newsList[1]),
    Course(cid: newsList[2].id, title: newsList[2].title, content: newsList[2].summary, color: Color.yellow, newsListItem: newsList[2]),
    Course(cid: newsList[3].id, title: newsList[3].title, content: newsList[3].summary, color: Color.green, newsListItem: newsList[3]),
    Course(cid: newsList[4].id, title: newsList[4].title, content: newsList[4].summary, color: Color.orange, newsListItem: newsList[4])
]

let sysColor :[Color] = [Color.orange,Color.blue,Color.red]

func recommendNews() -> Array<Course>{
    var i = 0
    var courseData:Array<Course> = []
    newsList.forEach{(item) in
        if findNewsbyId(item.id)[0].access >= 3 {
            courseData.append(Course(cid: item.id, title: item.title, content: item.summary, color: sysColor[i], newsListItem: item))
            i = i + 1
        }
    }
    return courseData
    
}
